import 'dart:convert';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';
import 'package:miuu_note_app/dao/todo_dao.dart';
import 'package:miuu_note_app/model/todo_entity.dart';

import '../../constant/todo_enum.dart';
import '../../generated/l10n.dart';
import '../../model/msg_entity.dart';
import '../dao/snippet_dao.dart';
import '../model/snippet_entity.dart';

class SnippetAdd extends StatefulWidget {
  const SnippetAdd({super.key});
  @override
  State<SnippetAdd> createState() => _SnippetAddState();
}

class _SnippetAddState extends State<SnippetAdd> {
  SnippetModel snippetModel = SnippetModel();
  String tilte = '';

  final TextEditingController _titleController = TextEditingController()
    ..text = '';
  final TextEditingController _contentController = TextEditingController()
    ..text = '';

  final TextEditingController _introController = TextEditingController()
    ..text = '';

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      Object? arguments = ModalRoute.of(context)?.settings.arguments;
      Map map = arguments as Map;
      setState(() {
        tilte = map['title'];
        snippetModel = SnippetModel.fromJson(map['snippet']);
        _titleController.text = snippetModel.title!;
        _contentController.text = snippetModel.content!;
        _introController.text = snippetModel.intro!;
      });
    });
  }

  @override
  void dispose() {
    //将要销毁的方法或数据放在这里
    super.dispose();

    _titleController.dispose();
    _contentController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        BrnLocalizationDelegate.delegate,
      ],
      // 应用支持的语言列表
      supportedLocales: S.delegate.supportedLocales,
      color: Colors.blue,
      home: Scaffold(
          body: ListView(
        children: [
          BrnNormalFormGroup(
            title: "新增随记",
            children: [
              BrnRadioInputFormItem(
                title: "类型",
                options: const [
                  "工作",
                  "学习",
                  "代码",
                  "备忘",
                ],
                value: SnippetType.getLabelByName(snippetModel.type ?? 'work'),
                onChanged: (oldValue, newValue) {
                  snippetModel.type = SnippetType.getNameByLabel(newValue!);
                  // BrnToast.show(
                  //     "点击触发回调${oldValue}_${newValue}_onChanged", context);
                },
              ),
              BrnTextInputFormItem(
                controller: _titleController,
                title: "标题",
                hint: "请输入标题",
                isRequire: true,
                onChanged: (newValue) {
                  snippetModel.title = newValue;
                },
              ),
              BrnTextInputFormItem(
                controller: _introController,
                title: "描述",
                hint: "请输入描述",
                isRequire: true,
                onChanged: (newValue) {
                  snippetModel.intro = newValue;
                },
              ),
              BrnTextBlockInputFormItem(
                controller: _contentController,
                title: "内容",
                hint: "请输入内容",
                isRequire: true,
                minLines: 1,
                maxLines: 5,
                onChanged: (newValue) {
                  setState(() {
                    snippetModel.content = newValue;
                  });
                },
              ),
              BrnBottomButtonPanel(
                mainButtonName: '提交',
                mainButtonOnTap: () {
                  _saveOrUpdateSnippet(snippetModel.toJson()).then((value) {
                    BrnToast.show(
                        value!.msgModel.msg ?? '操作异常，请联系管理员', context);
                    if (value.msgModel.code == 200) {
                      Navigator.pop(context, value.msgModel);
                    }
                  });
                },
              )
            ],
          ),
        ],
      )),
    );
  }

  Future<MsgEntity?> _saveOrUpdateSnippet(Map<String, dynamic> map) async {
    return await SnippetSaveOrUpdateDao.fetch(map);
  }
}
